import { ElMessage } from 'element-plus'
import { orderFoodHurry } from '@/api/food'
import messageTips from '@/components/messageTips'

// 单品菜品操作相关的hook
export function useSingleFoodActions() {
    // 单品催菜处理
    const handleSingleFoodHurry = async (currentDeskDetail: any, emit: any, selectFoodActiveIndex: any) => {
        if (!currentDeskDetail) {
            ElMessage.warning('请先选择桌台')
            return
        }

        // 检查是否选择了菜品
        if (selectFoodActiveIndex === -1) {
            ElMessage.warning('请先选择菜品')
            return
        }

        // 获取选中的菜品信息
        const selectedFood = currentDeskDetail.foodList?.[selectFoodActiveIndex]

        if (!selectedFood) {
            ElMessage.warning('选中的菜品信息异常')
            return
        }

        try {
            const messageInstance = await messageTips.message(
                {
                    title: '催菜',
                    content: `确定对（${selectedFood.name}）催菜，并远程打印催菜通知单吗？`,
                    isOnCancel: true,
                    onCancelText: '只催菜',
                    onSumbitText: '催菜并打印'
                },
                // 确认回调：催菜并打印
                async () => {
                    try {
                        messageInstance.setLoading(true)

                        const params = {
                            id: selectedFood.id,
                            print: 1 // 打印标志
                        }

                        const res = await orderFoodHurry(params)
                        if (res.status === 200) {
                            ElMessage.success(res.data?.message || '催菜并打印成功')
                            emit('refreshData')
                            emit('refreshDesk')
                        } else {
                            ElMessage.error(res.data?.message || '催菜失败')
                        }
                    } catch (error) {
                        console.error('催菜失败:', error)
                        ElMessage.error('催菜失败')
                    } finally {
                        messageInstance.setLoading(false)
                        messageInstance.close && messageInstance.close()
                    }
                },
                // 取消回调：只催菜不打印
                async () => {
                    try {
                        messageInstance.setLoading(true)

                        const params = {
                            id: selectedFood.id,
                            print: 0 // 不打印标志
                        }

                        const res = await orderFoodHurry(params)
                        if (res.status === 200) {
                            ElMessage.success(res.data?.message || '催菜成功')
                            emit('refreshData')
                            emit('refreshDesk')
                        } else {
                            ElMessage.error(res.data?.message || '催菜失败')
                        }
                    } catch (error) {
                        console.error('催菜失败:', error)
                        ElMessage.error('催菜失败')
                    } finally {
                        messageInstance.setLoading(false)
                        messageInstance.close && messageInstance.close()
                    }
                }
            )
        } catch (error) {
            // 这里处理的是弹窗被关闭的情况（比如点击遮罩层关闭）
            console.log('弹窗被关闭:', error)
        }
    }

    // 单品等叫处理
    const handleSingleFoodWait = async (currentDeskDetail: any, emit: any, selectFoodActiveIndex: any) => {
        // 这里只需要调用WaitFood.vue组件的方法，不需要实现具体逻辑
        // WaitFood.vue组件会处理等叫的逻辑
    }

    // 单品退菜处理
    const handleSingleFoodBack = async (currentDeskDetail: any, emit: any, selectFoodActiveIndex: any) => {
        // 实现退菜功能
    }

    // 单品赠送/撤赠处理
    const handleSingleFoodPresent = async (currentDeskDetail: any, emit: any, selectFoodActiveIndex: any) => {
        // 实现赠送/撤赠功能
    }

    return {
        handleSingleFoodHurry,
        handleSingleFoodWait,
        handleSingleFoodBack,
        handleSingleFoodPresent
    }
}
